﻿using System;
using System.Reflection;
using Inovout.Descriptors;
using Inovout.Models;

namespace Inovout.NumberGenerator
{
    public partial class NumberDefinition:DataObject
    {
        private object scope;
        private Type scopeType;
        private string scopeIdentity;

        public object Scope
        {
            get
            {
                if (scope == null)
                {
                    if (scopeType.IsEnum)
                    {
                        EnumDescriptor enumDescriptor = EnumDescriptor.Create(scopeType);
                        scope = enumDescriptor.GetEnum(scopeIdentity);
                    }
                    else
                    {
                        throw new NotSupportedException("暂不支持直接通常ID获取DataObject对象实例。");
                    }
                }
                return scope; 
            }
            set 
            {
                scope = value;
                //减少一次IF判断
                scopeType = scope.GetType();
          }
        }
        public Type ScopeType
        {
            get { return scopeType; }
        }
        public string ScopeIdentity
        {
            get 
            {
                //因为在关联对象时，ID并未生成。
                if (scope is DataObject)
                {
                    scopeIdentity = ((DataObject)scope).Id.ToString();
                }
                else
                {
                    scopeIdentity = scope.ToString();
                }
  
                return scopeIdentity; 
            }
        }
        private PropertyInfo objectProperty;
        private Type objectType;
        private string objectPropertyName;

        public PropertyInfo ObjectProperty
        {
            get 
            {
                if (objectProperty == null)
                {
                    objectProperty = objectType.GetProperty(objectPropertyName);
                }
                return objectProperty; 
            }
            set 
            {
                objectProperty = value;
                objectType = objectProperty.DeclaringType;
                objectPropertyName = objectProperty.Name;
            }
        }
        public Type ObjectType
        {
            get { return objectType; }
        }
        public string ObjectPropertyName
        {
            get { return objectPropertyName; }
        }
        private NumberExpression numberExpression;
        private string numberExpressionString;
        public NumberExpression NumberExpression
        {
            get { return numberExpression; }
            set 
            {
                numberExpression = value;
                numberExpressionString = numberExpression.Statement;
            }
        }
        public string NumberExpressionString
        {
            get { return numberExpressionString; }
        }
    }
}
